package 笔试.贝壳;

import java.util.Set;
import java.util.TreeSet;

public class Demo2 {
    public static void main(String[] args) {
        //Scanner sc = new Scanner(System.in);
        System.out.println(NS_String("caabeefa", 2));
    }

    //"caabeefa",2
    public static String NS_String (String s, int k) {
        // write code here
       if(k<=0){
           return s;
       }
       if(k>=26){
           return "";
       }
       char[] chars = s.toCharArray();
        Set<Character> set = new TreeSet<>();
        for(char c :chars){
            set.add(c);
        }
        if(set.size()<=k){
            return "";
        }
        //记录前k个字符
        Set<Character> set2 = new TreeSet<>();
        int cnt = 0;
        for(Character c:set){
            if(cnt==k){
                break;
            }
            set2.add(c);
            cnt++;
        }
        StringBuilder sb = new StringBuilder();
        for(char c:chars){
            if(!set2.contains(c)){
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
